package kr.hkit.androidcomplete03.ch22;

import java.util.Random;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class Ball {
	// 볼의좌표
	int x;
	int y;
	// 볼의 크기
	int rad;
	// 볼의 이동방향
	int dx;
	int dy;
	// 볼의 색
	int color;
	// 볼의 벽에 부딪히는 횟수
	int count;
	Paint paint;
	
	static Ball create(int x, int y) {
		Random rnd = new Random();
		Ball newBall = new Ball();
		newBall.paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		newBall.x = x;
		newBall.y = y;
		newBall.rad = rnd.nextInt(50);
		do {
			newBall.dx = rnd.nextInt(11) - 5;
			newBall.dy = rnd.nextInt(11) - 5;
		} while (newBall.dx == 0 || newBall.dy == 0);

		newBall.count = 0;
		newBall.color = Color.rgb(rnd.nextInt(256), rnd.nextInt(256),
				rnd.nextInt(256));
		return newBall;
	}

	void Move(int Width, int Height) {
		x += dx;
		y += dy;

		if (x < rad || x > Width - rad) {
			dx = dx * -1; // 벽에 부딪쳤을때 방향 전환
			count++;
		}
		if (y < rad || y > Height - rad) {
			dy = dy * -1; // 벽에 부딪쳤을때 방향 전환
			count++;
		}
	}
	void Draw(Canvas canvas){
		/*Paint pnt =new Paint();*/
		
		for(int r=rad,alpha=1; r>4; r--,alpha +=5){
			paint.setColor(Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color)));
			canvas.drawCircle(x, y, r, paint);
			
		}
	}
}